package com.theydream.qeechain.main;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.papaya.social.PPYFeedView;
import com.papaya.social.PPYSession;
import com.papaya.social.PPYSocial;
import com.theydream.qeechain.R;
import com.theydream.qeechain.ARcaught.ARClick;
import com.theydream.qeechain.battle.Battle;
import com.theydream.qeechain.globaldata.GlobalData;
import com.theydream.qeechain.map.Map;
import com.theydream.qeechain.publicmethod.PublicMethod;

/**
 * Main Menu Screen
 */
public class HomeMenuScreen extends Activity implements PPYSocial.Delegate{

	private PPYFeedView feedPPY;
	private static final int ID_HOME     = 1;
	private static final int ID_PET   = 2;
	private static final int ID_MAP = 3;
	private static final int ID_COLLECTED   = 4;
	private static final int ID_MISSION  = 5;	
	
	private Animation mapButton_Animation;
	// ================================================================
	// Inherited methods
	// ================================================================

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		feedPPY=(PPYFeedView)findViewById(R.id.entrance);
		GlobalData.initialize(this.getApplicationContext());
		GlobalData.getInstance().getDbhelper().loadPetInfo();
		GlobalData.getInstance().getDbhelper().loadMapItem();
		GlobalData.getInstance().getDbhelper().loadCaughtPets();
		GlobalData.getInstance().getDbhelper().loadSkill();
		//Use this two line to set My Pet Image on Main Menu
		ImageView pet=(ImageView) findViewById(R.id.home_myqee);
		pet.setImageResource(getResources().getIdentifier(GlobalData.getInstance().getPetInUse().get(0).getPic()+"_right", "drawable", this.getPackageName()));
		//checkInternetConnection() ;
		final ImageView mapButton=(ImageView) findViewById(R.id.home_map);
		mapButton_Animation=AnimationUtils.loadAnimation(this, R.anim.home_button_effect);
		mapButton.startAnimation(mapButton_Animation);
		mapButton_Animation.setAnimationListener(new Animation.AnimationListener() {
			public void onAnimationEnd(Animation animation) {
				mapButton_Animation=AnimationUtils.loadAnimation(HomeMenuScreen.this, R.anim.home_button_effect);
				mapButton_Animation.setAnimationListener(this);
				mapButton.startAnimation(mapButton_Animation);
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
			}
	});
		PPYSocial.addDelegate(this);
//		ActionItem nextItem 	= new ActionItem(ID_HOME, "Home", getResources().getDrawable(R.drawable.quickaction_home));
//		ActionItem prevItem 	= new ActionItem(ID_PET, "Pet", getResources().getDrawable(R.drawable.pet_silver));
//        ActionItem searchItem 	= new ActionItem(ID_MAP, "Adventure", getResources().getDrawable(R.drawable.quickaction_search));
//        ActionItem infoItem 	= new ActionItem(ID_COLLECTED, "Pocket", getResources().getDrawable(R.drawable.collection));
//        ActionItem eraseItem 	= new ActionItem(ID_MISSION, "Quest", getResources().getDrawable(R.drawable.quickaction_quest));
	}
	
	@Override
	public void onResume()	{
		super.onResume();
		ImageView pet=(ImageView) findViewById(R.id.home_myqee);
		pet.setImageResource(getResources().getIdentifier(GlobalData.getInstance().getPetInUse().get(0).getPic()+"_right", "drawable", this.getPackageName()));
		feedPPY.setRunThread(true);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		PPYSocial.removeDelegate(this);
	}

	protected void onPause() {
		super.onPause();
		feedPPY.setRunThread(false);
	}

	public void onSessionUpdated() {
		toastSession();
	}

	public void onScoreUpdated() {
		toastSession();
	}

	public void onAccountChanged(int oldUID, int newUID) {

	}
	private void toastSession() {
		PPYSession session = PPYSession.getInstance();
		String msg = String.format(getResources().getString(R.string.text_session_toast), session.getUID(),
				session.getNickname(), session.getScores());
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	public void toMap(View target)
	{
		Intent intent=new Intent(this,Map.class);
		startActivity(intent);
	}
	public void toAR(View target)
	{
		Intent intent=new Intent(this,ARClick.class);
		startActivity(intent);
	}
	public void toCollection(View target)
	{
		Intent intent=new Intent(this,Collected.class);
		startActivity(intent);
	}
	public void toMyQee(View target)
	{
		Intent intent=new Intent(this,MyPet.class);
		startActivity(intent);
	}
	public void toTutorial(View target)
	{
		Intent intent=new Intent(this,Tutorial.class);
		startActivity(intent);
	}
	
	public void toBoss(View target)
	{
		if (PublicMethod.canFightBoss())
		{
			Intent intent=new Intent(this,Battle.class);
			GlobalData.getInstance().setArID(Integer.toString(GlobalData.getInstance().getPetList().size()));
			Bundle bundle = new Bundle();
			bundle.putString("eid", Integer.toString(GlobalData.getInstance().getPetList().size()));
			intent.putExtras(bundle);
			startActivity(intent);
		}
		else
		{
			Intent intent=new Intent(this,BossPage.class);
			startActivity(intent);
		}
		
	}
	public void checkInternetConnection() 
	{
	    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
	    // test for connection
	    if (cm.getActiveNetworkInfo() != null
	            && cm.getActiveNetworkInfo().isAvailable()
	            && cm.getActiveNetworkInfo().isConnected()) {
	    	
	        
	    } 
	    else {
	        Log.i("Connection", "Internet Connection Not Present");
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("3G or WiFi Network Connection Needed")
				       .setCancelable(true)
				       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				        	   dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
	    }
	}

}